<template>
  <div class="app-container">
    <div class="filter-container clearfix">
      <el-button v-if="checkPermission(permissionRoles.actor_rename_add)" class="filter-item" type="primary" icon="el-icon-plus" @click="handleAdd">添加操作</el-button>
      <el-select v-model="listQuery.type" class="filter-item" style="width: 140px;" clearable placeholder="选择类型">
        <el-option label="角色改名" value="1"> </el-option>
      </el-select>
      <el-select v-model="listQuery.sid" class="filter-item" style="width: 110px;" filterable clearable :loading="loading" placeholder="选择区服">
        <el-option
          v-for="item in serverList"
          :key="item.sid"
          :label="item.name+'('+item.sid+')'"
          :value="item.sid"
        >
        </el-option>
      </el-select>
      <el-date-picker v-model="listQuery.startDate" class="filter-item" type="date" style="width: 140px;" placeholder="操作时间"></el-date-picker>-
      <el-date-picker v-model="listQuery.endDate" class="filter-item" type="date" style="width: 140px;" placeholder="操作时间"></el-date-picker>
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="735">
      <el-table-column label="操作类型" width="110px" align="center">
        <template slot-scope="{row}">
          {{ row.type | typeFilter }}
        </template>
      </el-table-column>
      <el-table-column label="服务器" prop="sname" width="110px" align="center"></el-table-column>
      <el-table-column label="角色ID" prop="actor_id" width="150px" align="center"></el-table-column>
      <el-table-column label="角色名" prop="content" width="140px" align="center"></el-table-column>
      <el-table-column label="操作结果" prop="content_result" width="340px" align="center"></el-table-column>
      <el-table-column label="操作人" prop="create_user" width="60px" align="center"></el-table-column>
      <el-table-column label="操作时间" prop="create_dt" width="135px" align="center"></el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" title="添加操作" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="操作类型" prop="type">
          <el-select v-model="dataForm.type" class="filter-item" style="width: 140px;" clearable placeholder="选择操作类型">
            <el-option label="角色改名" value="1"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务器" prop="sid">
          <el-select v-model="dataForm.sid" class="filter-item" style="width: 140px;" filterable clearable placeholder="选择区服">
            <el-option
              v-for="item in serverList"
              :key="item.sid"
              :label="item.name+'('+item.sid+')'"
              :value="item.sid"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色ID" prop="actor_id">
          <el-input v-model="dataForm.actor_id" class="defaut-input" placeholder="填写角色ID" @blur="handleBlueActorId" />
        </el-form-item>
        <el-form-item label="角色信息">
          <el-form-item label="角色名">{{ actor.name }}</el-form-item>
          <el-form-item label="服务器名">{{ actor.sname }}</el-form-item>
        </el-form-item>
        <el-form-item label="新名称" prop="new_content">
          <el-input v-model="dataForm.new_content" class="defaut-input" placeholder="填写新名称" />
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { getServerList } from '@/api/ops'
import { checkActor } from '@/api/operator'
import { getRenameList, handleRename } from '@/api/actorInfo'

const defaultItem = {
  id: '',
  type: '',
  sid: '',
  actor_id: '',
  content: '',
  new_content: ''
}

const typeMap = {
  '1': '角色改名'
}
export default {
  name: 'ActorRename',
  components: { Pagination },
  directives: { waves },
  filters: {
    typeFilter(type) {
      return typeMap[type]
    }
  },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      list: [],
      total: 0,
      listLoading: false,
      loading: false,
      serverList: [],
      dataForm: Object.assign({}, defaultItem),
      rules: {
        type: [{ required: true, message: '请选择操作类型', trigger: 'blur' }],
        sid: [{ required: true, message: '请选择区服', trigger: 'blur' }],
        new_content: [{ required: true, message: '请填写新内容', trigger: 'blur' }]
      },
      actor: {
        name: '',
        sname: '',
        level: ''
      },
      listQuery: {
        startDate: '',
        endDate: '',
        type: '',
        page: 1,
        limit: 200
      },
      dialogVisible: false
    }
  },
  created() {
    this.getList()
    this.getServerList(false)
  },
  methods: {
    checkPermission,
    async getList() {
      const query = this.listQuery
      this.listLoading = true
      const res = await getRenameList(query)
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
    },
    handleFilter() {
      this.getList()
    },
    handleAdd() {
      this.actor.name = ''
      this.actor.sname = ''
      this.actor.level = ''
      this.dataForm = Object.assign({}, defaultItem)
      this.dialogVisible = true
      this.getServerList(false)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    async getServerList(params) {
      const query = params
      const res = await getServerList(query)
      this.loading = false
      this.serverList = res.data.items
    },
    handleBlueActorId() {
      if (this.dataForm.actor_id === '') return
      checkActor({ sid: this.dataForm.sid, actor_id: this.dataForm.actor_id }).then(response => {
        if (response.status) {
          const actor = response.data[0]
          this.actor.name = actor.name
          this.actor.sname = actor.sname
          this.actor.level = actor.level
          this.dataForm.content = actor.name
        }
      })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          handleRename(this.dataForm).then(response => {
            if (response.status) {
              this.dialogVisible = false
              this.getList()
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            } else {
              this.message.error(response.message)
            }
          })
        }
      })
    }
  }
}
</script>
